package com.ja.openchat.viewmodels;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ActivityUtils;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.setting.NotificationSettingsInfo;

import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;


public class NotificationSoundViewModel extends BaseViewModel {

    public ObservableField<Boolean> notifyShowChecked = new ObservableField<>(true);
    public ObservableField<Boolean> notifyVoiceChecked = new ObservableField<>(false);
    public ObservableField<Boolean> notifyViberateChecked = new ObservableField<>(true);

    public NotificationSoundViewModel(@NonNull Application application) {
        super(application);
        load();
    }

    private NotificationSettingsInfo notificationSettingsInfo = null;

    private void load(){
        TelegramManager.getTelegramManager().getNotificationSettings(ActivityUtils.getTopActivity(), new TgResultBlock<NotificationSettingsInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, NotificationSettingsInfo obj) {
                notificationSettingsInfo = obj;
                resetUI();
            }

            @Override
            public void onTimeout(Map<String, Object> request) {

            }
        });
    }

    public BindingCommand toggleNotifyShow = new BindingCommand(() -> {
        boolean isChecked = !notifyShowChecked.get();
//        SPUtils.put(getApplication(), Constants.SPU_FOR_NOTIFICATION_SHOW,isChecked);
//        notifyShowChecked.set(isChecked);
        if(notificationSettingsInfo==null){
            return;
        }
        notificationSettingsInfo.showNotification = isChecked;
        change();
    });
    public BindingCommand toggleNotifyVoice = new BindingCommand(() -> {
        boolean isChecked = !notifyVoiceChecked.get();
//        SPUtils.put(getApplication(), Constants.SPU_FOR_NOTIFICATION_VOICE,isChecked);
//        notifyVoiceChecked.set(isChecked);
        if(notificationSettingsInfo==null){
            return;
        }
        notificationSettingsInfo.inAppSound = isChecked;
        change();
    });
    public BindingCommand toggleNotifyViberate = new BindingCommand(() -> {
        boolean isChecked = !notifyViberateChecked.get();
//        SPUtils.put(getApplication(), Constants.SPU_FOR_NOTIFICATION_VIBRATE,isChecked);
//        notifyViberateChecked.set(isChecked);
        if(notificationSettingsInfo==null){
            return;
        }
        notificationSettingsInfo.inAppVibration = isChecked;
        change();
    });

    private void change(){
        if(notificationSettingsInfo==null){
            return;
        }
        showProgress();
        TelegramManager.getTelegramManager().modifyNotificationSettings(ActivityUtils.getTopActivity(), notificationSettingsInfo, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if(obj==null||!obj.ok){
                    showErrorTip("设置错误，请稍后重试");
                }else{
                    resetUI();
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip("设置超时，请稍后重试");
            }
        });
    }

    private void resetUI(){
//        notifyShowChecked.set(SPUtils.getBoolean(getApplication(), Constants.SPU_FOR_NOTIFICATION_SHOW,true));
//        notifyVoiceChecked.set(SPUtils.getBoolean(getApplication(), Constants.SPU_FOR_NOTIFICATION_VOICE,false));
//        notifyViberateChecked.set(SPUtils.getBoolean(getApplication(), Constants.SPU_FOR_NOTIFICATION_VIBRATE,true));
        if(notificationSettingsInfo!=null){
            notifyShowChecked.set(notificationSettingsInfo.showNotification);
            notifyVoiceChecked.set(notificationSettingsInfo.inAppSound);
            notifyViberateChecked.set(notificationSettingsInfo.inAppVibration);
        }
    }

    //点击事件
    public BindingCommand btnOnClickCommand = new BindingCommand(() -> {
        if(notificationSettingsInfo==null){
            return;
        }
        Activity activity = ActivityUtils.getTopActivity();
        AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
        dialog.setTitle("重置所有通知").setMessage("您确定要重置所有通知设置？");
        dialog.setPositiveButton("重置",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        notificationSettingsInfo.showNotification = true;
                        notificationSettingsInfo.inAppSound = true;
                        notificationSettingsInfo.inAppVibration = true;
                        change();
                    }
                });
        dialog.setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
    });
}
